//
//  HJUserManager.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJUserManager.h"

#define UserInfoFilePath ([[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"user_info.file"]])
#define TaskGamerInfoFilePath ([[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"TaskGamer_info.file"]])

@implementation HJUserManager

/// 同步个人信息
+ (void)syncUserModel {
    [self syncUserModelWithCompletion:nil];
}

+ (void)syncUserModelWithCompletion:(void (^)(HJUserModel *model))completion {
    [TZHttpTool post:InfoAPI params:nil success:^(NSDictionary *result) {
        [HJUserManager setUserModelWithDict:result[@"data"]];
        HJUserModel *model = [HJUserModel mj_objectWithKeyValues:result[@"data"]];
//        [self updatePushOptionsUserModel:model];
        if (completion) {
            completion(model);
        }
    } failure:nil];
}
/// 获得当前登录用户的个人信息
+ (HJUserModel *)getUserModel {
    NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithFile:UserInfoFilePath];
    HJUserModel *model = [HJUserModel mj_objectWithKeyValues:dict];
    model.uid = [NSString stringWithFormat:@"%@",[mUserDefaults objectForKey:@"uid"]];
    return model;
}

/// 设置当前登录用户的个人信息
+ (void)setUserModelWithDict:(NSDictionary *)dict {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"login"];
    [[NSUserDefaults standardUserDefaults] setObject:dict[@"uid"] forKey:@"uid"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    BOOL saveSuccess = [NSKeyedArchiver archiveRootObject:dict toFile:UserInfoFilePath];
    if (!saveSuccess) {
        NSLog(@"个人信息保存失败");
    }
}
/// 是否是登录状态,如果不是，弹出登录控制器
+ (BOOL)isLogin {
    BOOL isLogon = [[mUserDefaults objectForKey:@"isLogin"] isEqualToString:@"1"];
    if (!isLogon && ![[UIViewController className] isEqualToString:@"HJLoginViewController"]) {
        HJLoginViewController *vc = [[HJLoginViewController alloc] init];
        HJNavigationViewController *navi = [[HJNavigationViewController alloc] initWithRootViewController:vc];
        [[UIViewController currentViewController] presentViewController:navi animated:YES completion:nil];
    }
    return isLogon;
}
+(BOOL)isLoginView {
    if (![[mUserDefaults objectForKey:@"isLogin"] isEqualToString:@"1"]) {
        if (![[UIViewController className] isEqualToString:@"HJLoginViewController"]) {
            HJLoginViewController *vc = [[HJLoginViewController alloc] init];
            HJNavigationViewController *navi = [[HJNavigationViewController alloc] initWithRootViewController:vc];
            [[UIViewController currentViewController] presentViewController:navi animated:YES completion:nil];
            return YES;
        }
    }
    return NO;
}
@end


@implementation HJUserModel
+(NSDictionary *)mj_objectClassInArray {
    return @{ @"brand_list":[brandListModel class],@"default_size":[defaultSizeModel class]};
}


@end


@implementation brandListModel



@end


@implementation defaultSizeModel



@end
